package cn.kawa.mq._01sync;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.创建消费者对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("sync_consumer");
        //2.连接mq : 设置nameServer地址
        consumer.setNamesrvAddr("localhost:9876");
        //3.消费者订阅主题 subscribe（）
        consumer.subscribe("topic","tags");
        //4.注册监听器
        //new MessageListenerConcurrently() {   public ConsumeConcurrentlyStatus consumeMessage

        consumer.registerMessageListener((List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) -> {
                for (MessageExt messageExt : list) {
                    System.out.println(new String(messageExt.getBody(), StandardCharsets.UTF_8));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });

        //5.启动消费者
        consumer.start();
    }

}